package com.mtons.mblog.modules.repository;

import com.mtons.mblog.modules.entity.Quest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface QuestRepository extends JpaRepository<Quest, Long>, JpaSpecificationExecutor<Quest> {
    Page<Quest> findAllByAuthorId(Pageable pageable, long userId);

    @Query("select coalesce(max(weight), 0) from Quest")
    int maxWeight();

    @Modifying
    @Query("update Quest set views = views + :increment where id = :id")
    void updateViews(@Param("id") long id, @Param("increment") int identityStep);

    @Modifying
    @Query("update Quest set answers = answers + :increment where id = :id")
    void updateAnswers(@Param("id") long id, @Param("increment") int identityStep);

    List<Quest> findAllByStatus(int status, Sort sort);
}
